package com.cx.healthex.client.wechat.controller.act;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cx.base.apiModel.ApiModel;
import com.cx.base.apiModel.PageModel;
import com.cx.base.autoDoc.annotations.AutoDocField;
import com.cx.base.autoDoc.annotations.AutoDocMethod;
import com.cx.base.autoDoc.annotations.AutoDocType;
import com.cx.base.controller.BaseController;
import com.cx.base.utils.StringUtil;
import com.cx.healthex.client.wechat.dto.act.ActTemplateDTO;
import com.cx.healthex.client.wechat.dto.act.ActivityDTO;
import com.cx.healthex.client.wechat.dto.act.StorageDTO;
import com.cx.healthex.client.wechat.dto.consumer.ConsumerDTO;
import com.cx.healthex.client.wechat.service.ActivityService;
import com.cx.healthex.client.wechat.service.StorageService;
import com.cx.healthex.client.wechat.utils.PropertiesUtil;

/**
 * cenxi 2017-01-12
 * */
@RequestMapping("activity")
@Controller
@AutoDocType("活动信息")
public class ActivityController extends BaseController{
	
	@Resource
	private ActivityService activityService;
	
	@Resource
	private StorageService storageService;

	@RequestMapping("getActTemplate")
	@ResponseBody
	@AutoDocMethod(name = "查询活动模板列表",  desc="",resClazz = ActTemplateDTO.class,resSet="list",pageClazz=PageModel.class)
	public ApiModel getActTemplate(@AutoDocField("查询页数") Integer pageNum,@AutoDocField("返回条数") Integer itemCount){

		PageModel pageModel = activityService.queryActTemplates(pageNum, itemCount);
		
		return buildSuccMsg(pageModel);
	}
	
	@RequestMapping("saveOrUpdateActBasicInfo")
	@ResponseBody
	@AutoDocMethod(name = "保存或更新活动基本信息",  desc="templateId,actName,startTime,endTime 为必传参数；活动ID不为空则为更新",resClazz = ActivityDTO.class)
	public ApiModel saveOrUpdateActBasicInfo(ActivityDTO activity,HttpServletRequest request){
		
		if(activity != null){
			//读session
			ConsumerDTO dto = (ConsumerDTO)request.getSession().getAttribute(PropertiesUtil.getConfig().getString("LOGIN_FLAG"));
			if(dto != null){
				if(activity.getConsumerId() > 0){
					if(activity.getConsumerId() != dto.getConsumerId()){
						return buildErrorMsg(null, DATA_ERROR, "不是你的活动，不能更新");
					}
				}else{
					activity.setConsumerId(dto.getConsumerId());
				}
				
				ActivityDTO backActivity = activityService.saveOrUpdateActivity(activity);
				return buildSuccMsg(backActivity);
				
			}
		}
		
		return buildErrorMsg(null, DATA_ERROR, "创建或更新失败。");
	}
	
	@RequestMapping("getActBasicInfo")
	@ResponseBody
	@AutoDocMethod(name = "查询活动基本信息",  desc="actId 为必传参数",resClazz = ActivityDTO.class)
	public ApiModel getActBasicInfo(@AutoDocField("活动ID") Integer actId){
		ActivityDTO activity = activityService.queryActivityByActId(actId);
		
		return buildSuccMsg(activity);
	}
	
	@RequestMapping("getActs")
	@ResponseBody
	@AutoDocMethod(name = "查询客户活动列表",  desc="pageNum,itemCount 必传",resClazz = ActivityDTO.class,resSet="list")
	public ApiModel getActs(@AutoDocField("查询页数") Integer pageNum,@AutoDocField("返回条数") Integer itemCount,HttpServletRequest request){
		//读session
		ConsumerDTO dto = (ConsumerDTO)request.getSession().getAttribute(PropertiesUtil.getConfig().getString("LOGIN_FLAG"));
		if(dto != null){
			Integer consumerId = dto.getConsumerId();
			PageModel pm = activityService.queryActivity(pageNum, itemCount, consumerId);
			return buildSuccMsg(pm);
		}
		
		return buildSuccMsg(null);
	}
	
	@RequestMapping("saveOrUpdateActStorage")
	@ResponseBody
	@AutoDocMethod(name = "保存或更新活动样式",  desc="全部必传参数",resClazz = StorageDTO.class)
	public ApiModel saveOrUpdateActStorage(@AutoDocField("活动ID") Integer actId,@AutoDocField("唯一编号") String storageKey,@AutoDocField("样式内容") String storageValue){
		StorageDTO dto =  storageService.saveOrUpdateActStorage(actId, storageKey, storageValue);
		return buildSuccMsg(dto);
	}
	
	@RequestMapping("getActStorage")
	@ResponseBody
	@AutoDocMethod(name = "精准查询活动样式",  desc="actId storageKey 为必传参数",resClazz = StorageDTO.class)
	public ApiModel getActStorage(@AutoDocField("活动ID") Integer actId,@AutoDocField("唯一编号") String storageKey){
		//to do 根据活动ID与10相除，余数为多少，就到对应的分表查询数据
		if(actId > 0 && StringUtil.isNotNull(storageKey)){
			StorageDTO dto = storageService.getActStorage(actId, storageKey);
			return buildSuccMsg(dto);
		}

		return buildErrorMsg(null, DATA_ERROR, "获取查询活动信息出错");
	}
	
	@RequestMapping("getActStorages")
	@ResponseBody
	@AutoDocMethod(name = "查询活动所有样式",  desc="actId 为必传参数",resClazz = StorageDTO.class,resSet="list")
	public ApiModel getActStorages(@AutoDocField("活动ID") Integer actId,@AutoDocField("唯一编号") String storageKey){
		//to do 根据活动ID与10相除，余数为多少，就到对应的分表查询数据
		if(actId > 0){
			PageModel pm = storageService.getActStorages(actId);
			if(pm != null && pm.getItems().size() > 0){
				return buildSuccMsg(pm.getItems());
			}
			
			return buildSuccMsg(new ArrayList<StorageDTO>());
		}

		return buildErrorMsg(null, DATA_ERROR, "获取查询活动信息出错");
	}
}
